<?php
 

define('IN_SCRIPT',1);

/* Get all the required files and functions */
require_once('hd_settings.inc.php');
require_once('language/'.$hd_settings['language'].'.inc.php');
require_once('inc/common.inc.php');

/* Connect to database */
require_once('inc/database.inc.php');
hd_dbConnect() or hd_error("$hdlang[cant_connect_db] $hdlang[contact_webmsater] $hd_settings[webmaster_mail]!");

hd_session_start();
hd_isLoggedIn();
/* Must be administrator to access this page */
hd_isAdmin();

/* Print header */
require_once('inc/header.inc.php');

/* What should we do? */
$action=hd_input($_REQUEST['a']);
if ($action == 'new') {new_cat();}
elseif ($action == 'rename') {rename_cat();}
elseif ($action == 'remove') {remove();}
elseif ($action == 'order') {order_cat();}

/* Print main manage users page */
require_once('inc/show_admin_nav.inc.php');
?>

</td>
</tr>
<tr>
<td>

<script language="Javascript" type="text/javascript"><!--
function confirm_delete()
{
if (confirm('<?php echo $hdlang['confirm_del_cat']; ?>')) {return true;}
else {return false;}
}
//-->
</script>

<h3 align="center"><?php echo $hdlang['manage_cat']; ?></h3>

<p><?php echo $hdlang['cat_intro']; ?>.</p>

<div align="center">
<center>
<table border="0" cellspacing="1" cellpadding="3" class="white">
<tr>
<td class="admin_white"><?php echo $hdlang['cat_name']; ?></td>
<td class="admin_white"><?php echo $hdlang['cat_order']; ?></td>
<td class="admin_white">&nbsp;</td>
</tr>

<?php
$sql = "SELECT * FROM `hd_residences` ORDER BY `cat_order` ASC";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
$options='';
$i=1;

while ($mycat=hd_dbFetchAssoc($result))
{
    if ($i) {$color="admin_gray"; $i=0;}
    else {$color="admin_white"; $i=1;}

    /* Deleting residence with ID 1 (default residence) is not allowed */
    if ($mycat['id'] == 1)
    {
        $remove_code="&nbsp;";
    }
    else
    {
        $remove_code="<a href=\"manage_residences.php?a=remove&id=$mycat[id]\" onclick=\"return confirm_delete();\">$hdlang[remove]</a>";
    }

    $options .= "<option value=\"$mycat[id]\">$mycat[name]</option>\n";

echo <<<EOC
<tr>
<td class="$color">$mycat[name]</td>
<td class="$color" align="center"><a href="manage_residences.php?a=order&catid=$mycat[id]&move=-15">$hdlang[move_up]</a><br>
<a href="manage_residences.php?a=order&catid=$mycat[id]&move=15">$hdlang[move_dn]</a></td>
<td class="$color" align="center">$remove_code</td>
</tr>

EOC;
} // End while
?>
</table>
</center>
</div>

<p>&nbsp;</p>

<hr width="750">

<form action="manage_residences.php" method="POST">
<p align="center"><b><?php echo $hdlang['add_cat']; ?>:</b> (<?php echo $hdlang['max_chars']; ?>) <input type="text"
name="name" size="30" maxlength="40"><input type="hidden" name="a" value="new">
<input type="submit" value="<?php echo $hdlang['create_cat']; ?>" class="button"></p>
</form>

<hr width="750">

<form action="manage_residences.php" method="POST">
<p align="center"><?php echo $hdlang['ren_cat']; ?> <select name="catid"><?php
echo $options;
?></select> <?php echo $hdlang['to']; ?> <input type="text"
name="name" size="30" maxlength="40"><input type="hidden" name="a" value="rename">
<input type="submit" value="<?php echo $hdlang['ren_cat']; ?>" class="button"></p>
</form>

<p>&nbsp;</p>

<!-- HR -->
<p>&nbsp;</p>

<?php
require_once('inc/footer.inc.php');
exit();


/*** START FUNCTIONS ***/

function new_cat() {
global $settings, $hdlang;

$catname=hd_Input($_POST['name'],$hdlang['enter_cat_name']);

/* Get the latest cat_order */
$sql = "SELECT `cat_order` FROM `hd_residences` ORDER BY `cat_order` DESC LIMIT 1";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
$row = hd_dbFetchRow($result);
$my_order = $row[0]+10;

$sql = "INSERT INTO `hd_residences` (`name`,`cat_order`) VALUES ('$catname','$my_order')";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");

/* Print admin navigation */
require_once('inc/show_admin_nav.inc.php');
?>

</td>
</tr>
<tr>
<td>

<p>&nbsp;</p>
<h3 align="center"><?php echo $hdlang['cat_added']; ?></h3>

<p>&nbsp;</p>

<p align="center"><?php printf($hdlang['cat_name_added'],$catname); ?>!</p>

<p align="center"><a href="manage_residences.php"><?php echo $hdlang['manage_cat']; ?></a> |
<a href="admin_main.php"><?php echo $hdlang['main_page']; ?></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<!-- HR -->
<p>&nbsp;</p>

<?php
require_once('inc/footer.inc.php');
exit();
} // End new_cat()


function rename_cat() {
global $settings, $hdlang;

$catid=hd_isNumber($_POST['catid'],$hdlang['choose_cat_ren']);
$catname=hd_Input($_POST['name'],$hdlang['cat_ren_name']);

$sql = "UPDATE `hd_residences` SET `name`='$catname' WHERE `id`=$catid LIMIT 1";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
if (hd_dbAffectedRows() != 1) {hd_error("$hdlang[int_error]: $hdlang[cat_not_found].");}

/* Print admin navigation */
require_once('inc/show_admin_nav.inc.php');
?>

</td>
</tr>
<tr>
<td>

<p>&nbsp;</p>
<h3 align="center"><?php echo $hdlang['cat_renamed']; ?></h3>

<p>&nbsp;</p>

<p align="center"><?php echo $hdlang['cat_renamed_to'].' '.$catname; ?></p>

<p align="center"><a href="manage_residences.php"><?php echo $hdlang['manage_cat']; ?></a> |
<a href="admin_main.php"><?php echo $hdlang['main_page']; ?></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<!-- HR -->
<p>&nbsp;</p>

<?php
require_once('inc/footer.inc.php');
exit();
} // End rename_cat()


function remove() {
global $settings, $hdlang;

$mycat=hd_isNumber($_GET['id'],$hdlang['no_cat_id']);
if ($mycat == 1) {hd_error($hdlang['cant_del_default_cat']);}

$sql = "DELETE FROM `hd_residences` WHERE `id`=$mycat LIMIT 1";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
if (hd_dbAffectedRows() != 1) {hd_error("$hdlang[int_error]: $hdlang[cat_not_found].");}

/* Print admin navigation */
require_once('inc/show_admin_nav.inc.php');
?>

</td>
</tr>
<tr>
<td>

<p>&nbsp;</p>
<h3 align="center"><?php echo $hdlang['cat_removed']; ?></h3>

<p>&nbsp;</p>

<p align="center"><?php echo $hdlang['cat_removed_db']; ?>!</p>

<p align="center"><a href="manage_residences.php"><?php echo $hdlang['manage_cat']; ?></a> |
<a href="admin_main.php"><?php echo $hdlang['main_page']; ?></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<!-- HR -->
<p>&nbsp;</p>

<?php
require_once('inc/footer.inc.php');
exit();
} // End remove()


function order_cat() {
global $settings, $hdlang;

$catid=hd_isNumber($_GET['catid'],$hdlang['cat_move_id']);
$cat_move=intval($_GET['move']);

$sql = "UPDATE `hd_residences` SET `cat_order`=`cat_order`+$cat_move WHERE `id`=$catid LIMIT 1";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
if (hd_dbAffectedRows() != 1) {hd_error("$hdlang[int_error]: $hdlang[cat_not_found].");}

/* Update all residence fields with new order */
$sql = "SELECT `id` FROM `hd_residences` ORDER BY `cat_order` ASC";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");

$i = 10;
while ($mycat=hd_dbFetchAssoc($result))
{
    $sql = "UPDATE `hd_residences` SET `cat_order`=$i WHERE `id`=$mycat[id] LIMIT 1";
    hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
    $i += 10;
}

} // End order_cat()

?>
